from django.contrib import admin
from django.utils.translation import ugettext_lazy

from service_notes.models import *

# Register your models here.

class CallDetailInline(admin.TabularInline):
    model = CallDetail
    fields = ["message", "analysis", "create_time"]
    extra = 1


class AgeListFilter(admin.SimpleListFilter):
    # Human-readable title which will be displayed in the
    # right admin sidebar just above the filter options.
    title = ugettext_lazy('年龄段')

    # Parameter for the filter that will be used in the URL query.
    parameter_name = '选择'

    def lookups(self, request, model_admin):
        """
        Returns a list of tuples. The first element in each
        tuple is the coded value for the option that will
        appear in the URL query. The second element is the
        human-readable name for the option that will appear
        in the right sidebar.
        """
        return (
            ('20+', ugettext_lazy('20-30')),
            ('30+', ugettext_lazy('30-40')),
            ('40+', ugettext_lazy('40-50')),
            ('50+', ugettext_lazy('50-60')),
            ('60+', ugettext_lazy('60+')),
        )

    def queryset(self, request, queryset):
        """
        Returns the filtered queryset based on the value
        provided in the query string and retrievable via
        `self.value()`.
        """
        # Compare the requested value (either '80s' or '90s')
        # to decide how to filter the queryset.
        if self.value() == '20+':
            return queryset.filter(age__gte=20,age__lt=30)
        if self.value() == '30+':
            return queryset.filter(age__gte=30,age__lt=40)
        if self.value() == '40+':
            return queryset.filter(age__gte=40,age__lt=50)
        if self.value() == '50+':
            return queryset.filter(age__gte=50,age__lt=60)
        if self.value() == '60+':
            return queryset.filter(age__gte=60)


class CallNotesAdmin(admin.ModelAdmin):
    inlines = [CallDetailInline, ]
    list_display = ["phone", "name", "learned", "intention", "age", "sex", "school"]
    list_filter = [AgeListFilter, "sex", "learned", "intention",]
    search_fields = ["phone", "name", "school", "place"]
    

admin.site.register(CallNotes, CallNotesAdmin)